/*
 * @Copyright:  Copyright(C) 2018-2028 YTCK All rights reserved.
 * @Description:   
 * @Author: zyd
 * @LastEditors: zyd
 * @Date: 2019-01-26 08:58:53
 * @LastEditTime: 2019-04-20 12:02:43
 */

#include "dataPointTools.h"
#include "product.h"
#include "ringBuffer.h"

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

rb_t           rb1, rb2, rb3;                                               ///< Ring buffer structure variable
static uint8_t rbBuf1[RB_MAX_LEN], rbBuf2[RB_MAX_LEN], rbBuf3[RB_MAX_LEN];  ///< Ring buffer data cache buffer

/**
* @brief Write data to the ring buffer
* @param [in] pRb        :ringbuffer adress
* @param [in] buf        : buf adress
* @param [in] len        : byte length
* @return   correct : Returns the length of the written data
            failure : -1
*/
int32_t gizPutData(rb_t* pRb, uint8_t* buf, uint32_t len)
{
    int32_t count = 0;

    if (NULL == buf)
    {
        printf("ERR: gizPutData buf is empty \n");
        return -1;
    }

    count = rbWrite(pRb, buf, len);
    if (count != len)
    {
        printf("ERR: Failed to rbWrite \n");
        return -1;
    }

    return count;
}

/**
* @brief gizwits Protocol initialization interface
* Protocol-related timer, serial port initialization
* Datapoint initialization
* @param none
* @return none
* 初始化环形缓冲区
rb1--PM25 rb2--NULL rb3--Esp12F
*/
void ringBufferInit(void)
{
    rb1.rbCapacity = RB_PM_MAX_LAN;
    rb1.rbBuff = rbBuf1;
    if (0 == rbCreate(&rb1))
    {
        printf("rbCreate  rb1 Success \n");
    }
    else
    {
        printf("rbCreate rb1 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));

    rb2.rbCapacity = RB_MAX_LEN;
    rb2.rbBuff = rbBuf2;
    if (0 == rbCreate(&rb2))
    {
        printf("rbCreate  rb2 Success \n");
    }
    else
    {
        printf("rbCreate rb2 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));

    rb3.rbCapacity = RB_MAX_LEN;
    rb3.rbBuff = rbBuf3;
    if (0 == rbCreate(&rb3))
    {
        printf("rbCreate  rb3 Success \n");
    }
    else
    {
        printf("rbCreate rb3 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));
}
